package com.euq.controller;

import com.euq.dto.UserDTO;
import com.euq.entity.User;
import com.euq.log.BusinessLog;
import com.euq.service.UserService;
import com.euq.utils.SecurityUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public PageInfo<User> getUsers(@RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "10") int size) {
        return userService.getUsers(page, size);
    }
    @GetMapping ("/info")
    public ResponseEntity<?> getUserInfo() {
        return userService.getUserInfo();
    }
    @PostMapping("/select")
    public PageInfo<UserDTO> selectUsers(@RequestParam(defaultValue = "1") int page,
                                         @RequestParam(defaultValue = "5") int size,
                                         @RequestParam String account,
                                         @RequestParam String status,
                                         @RequestParam String name) {
        return userService.selectUsers(page,size, account, status, name);
    }
    @PostMapping("delete")
    @BusinessLog(logName = "用户删除",logType = "业务日志")
    public Object delete(Long id) throws Exception {
        return userService.deleteById(id);
    }
    @PostMapping("update")
    @BusinessLog(logName = "用户修改",logType = "业务日志")
    public User update(User user) {
        log.info("用户{}修改用户{}信息",SecurityUtils.getCurrentUser().getUsername(),user.getAccount());
        log.info("修改结果{}",user);
        return userService.update(user);
    }

    @PostMapping("changeStatus")
    @BusinessLog(logName = "修改权限",logType = "业务日志")
    public Object changeStatus(Long id, long status) throws Exception {
        log.info("用户{}修改ID为{}的用户权限为{}",SecurityUtils.getCurrentUser().getUsername(),id,status);
        return userService.changeStatus(id,status);
    }
    @PostMapping("resetPwd")
    @BusinessLog(logName = "重置密码",logType = "业务日志")
    public Object resetPwd(Long id) throws Exception {
        log.info("用户{}配置ID为{}的用户密码",SecurityUtils.getCurrentUser().getUsername(),id);
        return userService.resetPassword(id);
    }
    @PostMapping("setRole")
    @BusinessLog(logName = "配置权限",logType = "业务日志")
    public Object setRole(Long id, String roleId) throws Exception {
        log.info("用户{}配置ID为{}的用户权限为{}",SecurityUtils.getCurrentUser().getUsername(),id,roleId);
        return userService.setRole(id,roleId);
    }
}
